#ifndef DATALOADER_H
#define DATALOADER_H
#include <QFile>
#include "strategy.h"
#include <QDateTime>
// 数据加载器
class DataLoader {
public:
    DataLoader();
    static QVector<CandleData> loadFromCSV(const QString& filePath) {
        QVector<CandleData> data;
        QFile file(filePath);

        if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
            qWarning() << "无法打开文件:" << filePath;
            return data;
        }

        QTextStream in(&file);
        // 跳过表头
        QString header = in.readLine();

        int lineCount = 0;
        while (!in.atEnd()) {
            QString line = in.readLine();
            lineCount++;
            QStringList parts = line.split(',');

            if (parts.size() < 6) {
                qWarning() << "第" << lineCount << "行数据格式错误，跳过";
                continue;
            }

            bool ok;
            CandleData candle;

            // 解析时间戳（假设是Unix毫秒时间戳或格式化的日期时间）
            QString timestampStr = parts[0].trimmed();
            if (timestampStr.contains('-')) {
                // 格式化的日期时间 "yyyy-MM-dd HH:mm:ss"
                QDateTime dt = QDateTime::fromString(timestampStr, "yyyy-MM-dd HH:mm:ss");
                if (!dt.isValid()) {
                    qWarning() << "第" << lineCount << "行时间格式错误，跳过";
                    continue;
                }
                candle.timestamp = dt.toMSecsSinceEpoch();
            } else {
                // Unix时间戳（毫秒）
                candle.timestamp = timestampStr.toLongLong(&ok);
                if (!ok) {
                    qWarning() << "第" << lineCount << "行时间戳格式错误，跳过";
                    continue;
                }
            }

            candle.open = parts[1].toDouble(&ok);
            if (!ok) {
                qWarning() << "第" << lineCount << "行开盘价格式错误，跳过";
                continue;
            }

            candle.high = parts[2].toDouble(&ok);
            if (!ok) {
                qWarning() << "第" << lineCount << "行最高价格式错误，跳过";
                continue;
            }

            candle.low = parts[3].toDouble(&ok);
            if (!ok) {
                qWarning() << "第" << lineCount << "行最低价格式错误，跳过";
                continue;
            }

            candle.close = parts[4].toDouble(&ok);
            if (!ok) {
                qWarning() << "第" << lineCount << "行收盘价格式错误，跳过";
                continue;
            }

            candle.volume = parts[5].toDouble(&ok);
            if (!ok) {
                qWarning() << "第" << lineCount << "行成交量格式错误，跳过";
                continue;
            }

            data.append(candle);
        }

        file.close();

        // 按时间戳排序
        std::sort(data.begin(), data.end(), [](const CandleData& a, const CandleData& b) {
            return a.timestamp < b.timestamp;
        });

        qDebug() << "成功加载" << data.size() << "条K线数据";
        return data;
    }

    // 生成测试数据
    static QVector<CandleData> generateTestData(int days = 30, double startPrice = 50000.0) {
        QVector<CandleData> data;
        QDateTime startTime = QDateTime::currentDateTime().addDays(-days);

        double price = startPrice;
        srand(QDateTime::currentDateTime().toSecsSinceEpoch());

        for (int i = 0; i < days * 24 * 4; i++) { // 15分钟数据
            CandleData candle;
            candle.timestamp = startTime.addSecs(i * 15 * 60).toMSecsSinceEpoch();

            // 价格变动（-1% 到 +1%）
            double change = (rand() % 200 - 100) / 10000.0;
            double newPrice = price * (1 + change);

            candle.open = price;
            candle.close = newPrice;
            candle.high = qMax(price, newPrice) * (1 + rand() % 50 / 10000.0);
            candle.low = qMin(price, newPrice) * (1 - rand() % 50 / 10000.0);
            candle.volume = 1000 + rand() % 5000;

            data.append(candle);
            price = newPrice;
        }

        return data;
    }
};

#endif // DATALOADER_H
